package com.jweb.framework.core.channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jweb.framework.core.CoreRuntimeException;

public abstract class AbstractEndpointMapping<Request , Response> implements EndpointMapping<Request, Response> {
	
	protected Logger logger = LoggerFactory.getLogger(AbstractEndpointMapping.class);
	
	private Endpoint<Request, Response> endpoint ;
	
	public void setDefault(Endpoint<Request, Response> defaultEndpoint){
		endpoint = defaultEndpoint ;
	}
	
	public Endpoint<Request, Response> mapping(ChannelContext<Request, Response> channelContext){
		Endpoint<Request, Response> localEndpoint = doMapping(channelContext);
		if(localEndpoint == null && endpoint != null){
			localEndpoint = endpoint ;
			logger.warn("use default endpoint . ");
		}
		else if(logger.isDebugEnabled()){
			logger.debug("uri mapping endpoint : {}",localEndpoint);
		}
		if(localEndpoint == null){
			throw new CoreRuntimeException("JAVACO0006","no_endpoint_mapping_found.");
		}
		return localEndpoint ;
	}

	protected abstract Endpoint<Request, Response> doMapping(ChannelContext<Request, Response> channelContext);
	
}
